﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Clandestine.Conversations
{
    public class DialogueWindow
    {
        public ResizableBG NameBG;
        public ResizableBG MainBG;
        public Sprite PictureBG;
        public Sprite PictureFrame;
        public Sprite Picture;
        public TextSprite NameSprite;
        public Padding Padding = new Padding(5, 5, 5, 5);
        public string Name = string.Empty;

        // Lots of vars here to control the positioning, figured it was easier than hunting for numbers.
        Padding NamePadding;
        int NameHeight;
        int NameWidth;
        int NameSpacing;
        int PictureFrameWidth;
        int PictureSpacing;

        private bool visible = true;
        public bool Visible
        {
            get { return visible; }
            set
            {
                this.visible = value;
                NameBG.Visible = value;
                MainBG.Visible = value;
                PictureBG.Visible = value;
                PictureFrame.Visible = value;
                Picture.Visible = value;
                NameSprite.Visible = value;
            }
        }

        private Point position;
        public Point Position
        {
            get { return position; }
            set { position = value; }
        }

        public Point SpeechPosition
        {
            get { return new Point(Position.X + PictureFrameWidth + Padding.Left, Position.Y + NameHeight + Padding.Top); }
        }

        private Size speechSize = new Size(330, 68);
        public Size SpeechSize
        {
            get { return speechSize; }
            set { speechSize = value; }
        }

        // The position where menu's should be created.
        public Point MenuPosition
        {
            get { return new Point(this.Position.X + PictureFrameWidth + MainBG.Width - 5, PictureFrame.Y); }
        }

        public float Layer = 5f;
        // Difference between layers of the dialogue box.
        public const float LayerDifference = 0.01f;

        public static Size MinSpeechSize = new Size(330, 68);

        public DialogueWindow(string name, string picture, Point Position)
        {
            this.Position = Position;
            this.Name = name;
            this.NameSprite = Fonts.Default2.DrawText(name);
            this.Picture = new Sprite(picture);
            this.PictureBG = new Sprite("dialogue_face_bg.png");
            this.PictureFrame = new Sprite("dialogue_face_frame.png");

            this.MainBG = new ResizableBG();
            this.NameBG = new ResizableBG();
            this.Redraw();
            this.Visible = false;
        }

        public void Redraw()
        {
            NamePadding = new Padding(5, 5, 5, 5);
            NameHeight = NameSprite.Height + NamePadding.Top + NamePadding.Bottom; 
            NameWidth = NameSprite.Width + NamePadding.Left + NamePadding.Right;
            NameSpacing = 5; // Spacing between start of Speech box and start of Name box (X ordinate)
            PictureFrameWidth = PictureFrame.Width - 5; // -5 for underlap.
            PictureSpacing = 7; // Spacing between start of speech box and start of picture frame (Y ordinate)

            // Text is roughly 10px high, 5px padding, top and bottom and an extra 5px of the BG so it hides behind main
            // box.
            NameSprite.Position = new Point(Position.X + PictureFrameWidth + NameSpacing + NamePadding.Left, Position.Y + NamePadding.Top); // + 5 for spacing.
            NameSprite.Layer = Layer;
            NameSprite.GluedToCamera = true;
            NameBG.Redraw(new Point(Position.X + PictureFrameWidth + NameSpacing, Position.Y), new Size(NameWidth, NameHeight + 5)); // + 5 for underlap
            NameBG.Layer = Layer - LayerDifference;
            NameBG.GluedToCamera = true;

            // Picture stuff
            Point PicturePos = new Point(Position.X, Position.Y + NameHeight + PictureSpacing);
            PictureBG.Position = PicturePos; 
            PictureBG.Layer = Layer - (3f * LayerDifference);
            PictureBG.GluedToCamera = true;
            PictureFrame.Position = PicturePos;
            PictureFrame.Layer = Layer - (2f * LayerDifference);
            PictureFrame.GluedToCamera = true;
            Picture.Position = new Point(PicturePos.X + 3, PicturePos.Y + 3);
            Picture.Layer = Layer - LayerDifference;
            Picture.GluedToCamera = true;

            // Speech bubble
            MainBG.Redraw(new Point(Position.X + PictureFrameWidth, Position.Y + NameHeight), speechSize);
            MainBG.Layer = Layer;
            MainBG.GluedToCamera = true;
        }

        private void setVisible(bool visible)
        {
            
        }
    }
}
